<?php
/**
 * Created by PhpStorm.
 * User: xing.chen
 * Date: 2017/11/20
 * Time: 15:03
 */

namespace xing\helper\resource;


class HttpHelper
{
    public static $cert = '';

    public static $time = 30;

    public static $cookie = '';
    public static $method; 

    /**
     * 访问url
     * @param $url
     * @param array $post
     * @return string
     */
    public static function post($url, $post = [], $header = [])
    {
//        hr($url);
        $ch = static::init($url);
        if (!empty($post)) {


            $fields_string = is_array($post) ? http_build_query($post) : $post;
//            hr($fields_string);
//            er(http_build_query($post));
//            curl_setopt($ch, CURLOPT_POST, TRUE) ;
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string) ;
            curl_setopt($ch, CURLOPT_POST, TRUE);
//            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//            er($fields_string);
//            er($post);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST'); //设置请求方式
        } else {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET'); //设置请求方式
        }
        if (!empty(static::$method)) curl_setopt($ch, CURLOPT_CUSTOMREQUEST, static::$method); //设置请求方式

        if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHearders($header));

//        curl_setopt ($ch, CURLOPT_COOKIEJAR, static::$cookie); // 存放Cookie信息的文件名称
        static::$cookie && curl_setopt ($ch, CURLOPT_COOKIE, static::$cookie); // 读取上面所储
//        curl_setopt ($ch, CURLOPT_ENCODING, "" ); //设置为客户端支持gzip压缩


        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); // 获取的信息以文件流的形式
        curl_setopt ($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容

        //设置抓取跳转（http 301，302）后的页面
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        //设置最多的HTTP重定向的数量
        curl_setopt($ch, CURLOPT_MAXREDIRS, 2);

        $html = curl_exec($ch) ;
        curl_close($ch) ;

        return $html;
    }
    /**
     * @param $file_url
     * @param $save_to
     * @return bool
     */
    public static function downloadFile($url, $savePath)
    {
        $ch = static::init($url);
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $fileContent = curl_exec($ch);
        curl_close($ch);
        $downloadedFile = fopen($savePath, 'w');
        fwrite($downloadedFile, $fileContent);
        fclose($downloadedFile);
        return true;
    }

    public static function getFile($url)
    {
        $ch = static::init($url);
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            "accept:application/json, text/plain, */*",
            'accept-encoding:gzip, deflate, br',
            'accept-language:zh-CN,zh;q=0.8',
            'cache-control:no-cache',
            'pragma:no-cache',
            'user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
            'x-requested-with:XMLHttpRequest',
        ]);
        $fileContent = curl_exec($ch);

        curl_close($ch);
        return $fileContent;
    }

    public static function getImageType($url)
    {
        $ch = static::init($url);
// 获取头部信息
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        curl_exec($ch);
        curl_close($ch);
        $head = ob_get_contents();
        ob_end_clean();

        $regex = '/Content-Type:\s([\w-\/]+)/i';
        preg_match($regex, $head, $matches);

        return $matches[1] ?? null;
    }

    private static function init($url)
    {

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        //SSL证书
        if (preg_match('/https:/i',$url)){
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,!empty(static::$cert) ? true : false);
            !empty(static::$cert) && curl_setopt($ch,CURLOPT_CAINFO, static::$cert);
        }
        curl_setopt ($ch, CURLOPT_TIMEOUT, static::$time); // 设置超时限制防止死循环

        return $ch;

    }
    public static function getPostHttpBody($postFildes)
    {
        $content = "";
        foreach ($postFildes as $apiParamKey => $apiParamValue) {
            if (is_array($apiParamValue)) {
                $apiParamValue = static::getPostHttpBody($apiParamValue);
            }
            $content .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
        }
        return substr($content, 0, -1);
    }
    public static function getHttpHearders($headers)
    {
        $httpHeader = array();
        foreach ($headers as $key => $value) {
            array_push($httpHeader,  is_numeric($key) ? $value : $key.":".$value);
        }
        return $httpHeader;
    }
}
